package com.yesallchina.wxapp.officialaccount.api;

import com.yesallchina.wxapp.officialaccount.api.beans.ParamName;
import com.yesallchina.wxapp.officialaccount.service.WxOfficialAccountInitializer;
import com.yesallchina.wxapp.officialaccount.utils.WxHttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 菜单
 */
public class UIMenuApi {

    private final static Logger log = LoggerFactory.getLogger(UIMenuApi.class);

    private WxOfficialAccountInitializer wxInit;

    private BasicSupportApi basicSupport;

    public UIMenuApi(BasicSupportApi basicSupport) {
        this.basicSupport = basicSupport;
        this.wxInit = basicSupport.getWxInit();
    }

    /**
     * 创建菜单
     *
     * @param menu
     * @return
     */
    public String createMenu(String menu) {
        String params = ParamName.ACCESS_TOKEN + "=" + basicSupport.getAccessToken();
        String url = wxInit.getApiProp().getMenuCreate() + "?" + params;
        String response = WxHttpUtils.postJSON(url, menu, wxInit.getCommonProp());
        if (log.isDebugEnabled()) {
            log.debug("[创建公众号菜单]：{}", response);
        }
        return response;
    }

    /**
     * 查询菜单
     *
     * @return
     */
    public String queryMenu() {
        String params = ParamName.ACCESS_TOKEN + "=" + basicSupport.getAccessToken();
        String url = wxInit.getApiProp().getMenuGet() + "?" + params;
        String response = WxHttpUtils.postJSON(url, null, wxInit.getCommonProp());
        if (log.isDebugEnabled()) {
            log.debug("[查询公众号菜单]：{}", response);
        }
        return response;
    }

    /**
     * 删除菜单
     *
     * @return
     */
    public String delMenu() {
        String params = ParamName.ACCESS_TOKEN + "=" + basicSupport.getAccessToken();
        String url = wxInit.getApiProp().getMenuDelete() + "?" + params;
        String response = WxHttpUtils.postJSON(url, null, wxInit.getCommonProp());
        if (log.isDebugEnabled()) {
            log.debug("[删除公众号菜单]：{}", response);
        }
        return response;
    }


}
